package org.training.apr.dao.fake;

import java.util.List;

import org.training.apr.dao.UserDao;
import org.training.apr.model.Role;
import org.training.apr.model.User;
import org.training.apr.model.utils.builder.RoleBuilder;
import org.training.apr.model.utils.builder.UserBuilder;

public final class FakeUserDao extends FakeDao<User> implements UserDao {
	private static FakeUserDao instance = null;
	
	public static final FakeUserDao getInstance() {
		if(instance == null) {
			instance = new FakeUserDao();
		}
		return instance;
	}
	
	private FakeUserDao() {
		save(new UserBuilder().withUsername("operador").withPassword("").build());
		save(new UserBuilder().withUsername("director")
							  .withPassword("")
							  .withRole(new RoleBuilder().withType(Role.FINANCIAL_DIRECTOR).build())
							  .build());
	}

	public User readByUsername(final String username) {
		List<User> result = findByCriteria(new Criteria<User>() {
			public boolean evaluate(User instance) {				
				return instance.getUsername().equals(username);
			}
		});
		
		uniqueResult(result);
		if(result.size() == 0) {
			return null;
		}
		
		return result.get(0);
	}
	
	
}
